package cn.tedu.mvcemp.controller;

import cn.tedu.mvcemp.mappers.EmpMapper;
import cn.tedu.mvcemp.pojo.Emp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/emp")
public class EmpController {
@Autowired
private EmpMapper empMapper;

@RequestMapping("/insert")
    public String insertEmp(Emp emp){
    empMapper.insertEmp(emp);
    return "添加成功！<a href='/emp/select'>返回列表</a>";
}
@RequestMapping("/select")
    public String selectAllEmp(){
        List<Emp> list=empMapper.selectAllEmp();
        // 1.定义html字符串
        String html = "<table border='1'>";
        html += "<caption>员工列表</caption>";
        html += "<tr><th>id</th><th>姓名</th><th>工资</th><th>岗位</th><th>操作 </th></tr>";
        for (Emp emp: list) {
            html += "<tr>";
            html += "<td>" + emp.getId() + "</td>";
            html += "<td>" + emp.getTitle() + "</td>";
            html += "<td>" + emp.getSalary() + "</td>";
            html += "<td>" + emp.getJob() + "</td>";

            html += "<td><a href='/emp/delete?id=" + emp.getId() + "'>删除</a>" +
                    "</td>";
            html += "</tr>";
        }
        html += "</table>";


        return html+"<a href='/index.html'>首页</a>";
    }
    @RequestMapping("/update")
    public String updateEmp(Emp id){
    empMapper.updateEmp(id);
    return "修改完成！<a href='/emp/select'>返回列表</a>";
    }
    @RequestMapping("/delete")
    public String deleteEmp(int id){
    empMapper.deleteEmp(id);
    return "删除完成！<a href='/emp/select'>返回列表</a>";
    }
}
